package com.yizhi.controller;

import com.yizhi.domain.dto.LoginDTO;
import com.yizhi.domain.entityDO.UserDO;
import com.yizhi.service.LoginService;
import com.yizhi.util.ApiResult;
import com.yizhi.util.JwtTokenUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.security.auth.login.AccountNotFoundException;

/**
 * @author zhouyihe
 * @ClassName LoginController
 * @qq 1552951165
 */
@Api(tags = "用户登录控制器")
@Slf4j
@RestController
@RequestMapping
public class LoginController {
    @Autowired
    private LoginService loginService;
    
    @Autowired
     private JwtTokenUtil jwtTokenUtil;
    
    @PostMapping("/login")
    public ApiResult login(@RequestBody LoginDTO loginDTO) throws AccountNotFoundException {
        log.info("用户登录:{} ", loginDTO);
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();
        
        UserDO user = loginService.userLogin(username, password);
        
        String token = jwtTokenUtil.generateToken(user.getUsername());
        
        return ApiResult.success(token);
    }
}
